package com.briup.estore.web.servlet;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.extend.CategoryExtend;
import com.briup.estore.service.BookService;

import com.briup.estore.service.CategoryService;
import com.briup.estore.service.Impl.BookServiceImpl;
import com.briup.estore.service.Impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/index_data")
public class IndexDataServlet extends HttpServlet {
    private BookService bookService;
    private CategoryService categoryService;
    {
        bookService =new BookServiceImpl();
        categoryService =new CategoryServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> allBooks = bookService.findAllBooks();
        List<CategoryExtend> categoryExtends = categoryService.selectAll();
        HttpSession session = req.getSession();
        session.setAttribute("allBooks",allBooks);
        session.setAttribute("categoryList",categoryExtends);
        //resp.getWriter().println(allBooks);
        resp.sendRedirect("index");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
